package com.hitown.controller.dxhd;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hitown.annotation.LoginLog;
import com.hitown.model.dxhd.DxhdJq;
import com.hitown.model.dxhd.DxhdRybs;
import com.hitown.service.dxhd.DxhdJqService;
import com.hitown.util.Params;
import com.hitown.util.R;
import com.hitown.util.StringUtils;
import com.hitown.util.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.util.Base64;
import java.util.Collection;
import java.util.Date;
import java.util.UUID;


/**
 * <p>
 * 警情数据信息 前端控制器
 * </p>
 *
 * @author menu
 * @since 2021-07-19
 */
@Api(tags = {"DxhdJqController"},value = "警情数据信息",description = "警情数据信息相关接口说明")
@RestController
@RequestMapping("/dxhd-jq")
public class DxhdJqController {

    @Autowired
    private DxhdJqService dxhdJqService;

//    @LoginLog(content="警情列表查询" ,module = "大型活动")
    @CrossOrigin
    @ApiOperation(value = "分页查询")
    @RequestMapping("selectPage")
    public R selectPage(@ApiIgnore @RequestBody Params<DxhdJq> params){
        Page<DxhdJq> page = new Page<>(params.getCurrent(),params.getSize());
        QueryWrapper<DxhdJq> wrapper =new QueryWrapper<>();
        //警情名称
        if(!StringUtils.isEmpty(params.getEntity().getJqmc())){
            wrapper.like("JQMC",params.getEntity().getJqmc());
        }

        //报警人-姓名
        if(!StringUtils.isEmpty(params.getEntity().getBjrXm())){
            wrapper.like("BJR_XM", params.getEntity().getBjrXm());
        }

        //案事件发生开始时间
        if(!StringUtils.isEmpty(params.getEntity().getAsjfskssj())) {
            wrapper.eq("ASJFSKSSJ",params.getEntity().getAsjfskssj());

        }
        wrapper.orderByDesc("CTM");

        IPage<DxhdJq> pageVo = dxhdJqService.page(page,wrapper);
        for (DxhdJq e: pageVo.getRecords()) {
            if(e.getChlChltp()!=null){
                String base64Str = "data:image/jpg;base64,"+ DatatypeConverter.printBase64Binary(e.getChlChltp());
                e.setChlChltpDz(base64Str);
            }
        }
        return R.ok().put("data",pageVo);

    }

    @CrossOrigin
    @ApiOperation(value = "根据id查询记录")
    @RequestMapping("getById")
    public R getById(@RequestParam String Id){
        DxhdJq result = dxhdJqService.getById(Id);
        return R.ok().put("data",result);

    }

    @CrossOrigin
    @ApiOperation(value = "保存单条记录")
    @PostMapping("save")
    public R save(@RequestBody DxhdJq entit){
        entit.setJqbs(UUIDUtils.getUUID());
        Boolean result = dxhdJqService.save(entit);
        return R.ok().put("data",result);
    }


    @ApiOperation(value = "批量保存")
    @RequestMapping("saveBatch")
    public R saveBatch(@RequestBody Collection<DxhdJq> entityList){
        Boolean result = dxhdJqService.saveBatch(entityList);
        return R.ok().put("data",result);

    }

    @CrossOrigin
    @ApiOperation(value = "根据 ID 选择修改")
    @RequestMapping("updateById")
    public R updateById(@RequestBody DxhdJq entit){
        entit.setUtm(new Date());
        entit.setChlChltp(null);
        entit.setChlChltpDz(null);
        Boolean result = dxhdJqService.updateById(entit);
        return R.ok().put("data",result);

    }

    @ApiOperation(value = "根据 ID 选择修改")
    @RequestMapping("update")
    public R update(@RequestParam String jqbs,@RequestParam("file") MultipartFile file) throws IOException {
        DxhdJq entity = new DxhdJq();
        entity.setChlChltp(file.getBytes());
        if(!StringUtils.isEmpty(jqbs) && !jqbs.equals("undefined")){
            entity.setJqbs(jqbs);
            dxhdJqService.updateById(entity);
        }else{
            entity.setJqbs(UUID.randomUUID().toString());
            dxhdJqService.save(entity);
        }
        return R.ok().put("data",entity.getJqbs());
    }

    @ApiOperation(value = "根据 ID 选择修改")
    @RequestMapping("handleRemove")
    public R handleRemove(@RequestBody DxhdJq entity) throws IOException {
        entity.setChlChltp(new byte[0]);
        dxhdJqService.updateById(entity);
        return R.ok().put("data",entity.getJqbs());
    }

    @ApiOperation(value = "根据ID 批量更新")
    @RequestMapping("updateBatchById")
    public R updateBatchById(@RequestBody Collection<DxhdJq> entityList){
        Boolean result = dxhdJqService.updateBatchById(entityList);
        return R.ok().put("data",result);

    }

//    @LoginLog(content="警情列表" ,module = "大型活动")
    @CrossOrigin
    @ApiOperation(value = "根据 ID 删除")
    @RequestMapping("removeById")
    public R removeById(@RequestParam String Id){
        Boolean result = dxhdJqService.removeById(Id);
        return R.ok().put("data",result);

    }


    @ApiOperation(value = "删除（根据ID 批量删除）")
    @RequestMapping("removeByIds")
    public R removeByIds(@RequestBody Collection<String> Ids){
        Boolean result = dxhdJqService.removeByIds(Ids);
        return R.ok().put("data",result);
    }

}

